package cn.openea.perpartner.payment.web.points.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.payment.web.points.dto.PointsDto;
import cn.openea.perpartner.payment.web.points.entity.AddPoints;
import cn.openea.perpartner.payment.web.points.mapper.AddPointsMapper;
import cn.openea.perpartner.payment.web.points.service.IAddPointsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-10-27 14:32
 */
@Service
public class AddPointsServiceImpl extends ServiceImpl<AddPointsMapper, AddPoints> implements IAddPointsService {
    @Override
    public List<AddPoints> selectList(PointsDto dto) {
        LambdaQueryWrapper<AddPoints> wrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(dto.getSceneId())){
            wrapper.eq(AddPoints::getSceneId,dto.getSceneId());
        }
        if (StrUtil.isNotBlank(dto.getUserId())){
            wrapper.eq(AddPoints::getUserId,dto.getUserId());
        }
        if (StrUtil.isNotEmpty(dto.getSourceId())) {
            wrapper.eq(AddPoints::getSourceId, dto.getSourceId());
        }
        wrapper.orderByDesc(AddPoints::getCreateTime);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public boolean add(PointsDto pointsDto) {
        AddPoints addPoints = new AddPoints();
        BeanUtil.copyProperties(pointsDto,addPoints,true);
        return save(addPoints);
    }

    @Override
    public double getCumulativePoints(PointsDto pointsDto) {
        return baseMapper.getCumulativePoints(pointsDto.getUserId());
    }
}
